package org.tobaccomain.controller;


import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.tobaccomain.mapper.DeskMapper;
import org.tobaccomain.mapper.TobaccoDetailMapper;
import org.tobaccomain.pojo.dto.DownloadParam;
import org.tobaccomain.pojo.dto.PageTobacco;
import org.tobaccomain.pojo.dto.RecommendBody;
import org.tobaccomain.pojo.dto.TobaccoInfo;
import org.tobaccomain.pojo.entity.Result;
import org.tobaccomain.pojo.entity.Rfid;
import org.tobaccomain.pojo.entity.TobaccoBasic;
import org.tobaccomain.pojo.entity.TobaccoDetail;
import org.tobaccomain.pojo.vo.PageResult;
import org.tobaccomain.service.TobaccoService;
import org.tobaccomain.utils.RfidUtils;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@RestController
public class TobaccoController {
    @Resource
    private TobaccoService tobaccoService;
    @Resource
    private DeskMapper deskMapper;
    @Resource
    //创建一个线程池
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    @Resource
    private TobaccoDetailMapper tobaccoDetailMapper;

    //添加单料烟基本信息
    @PostMapping("/tobacco/basic")
    public Result addTobaccoBasic(@RequestBody TobaccoBasic tobaccoBasic){
        tobaccoService.addTobaccoBasic(tobaccoBasic);
        return Result.success("信息添加成功");
    }

    //删除单料烟信息
    @DeleteMapping("/tobacco")
    public Result delTobacco(TobaccoBasic tobaccoBasic){
        tobaccoService.delTobacco(tobaccoBasic);
        return Result.success("信息删除成功");
    }

    //修改单料烟备注信息
    @PutMapping("/tobacco/info")
    public Result modifyTobaccoInfo(@RequestBody TobaccoInfo tobaccoInfo){
        tobaccoService.modifyTobaccoInfo(tobaccoInfo);
        return Result.success("备注修改成功");
    }

    //修改单料烟基本信息
    @PutMapping("/tobacco/basic")
    public Result modifyTobaccoBasicInfo(@RequestBody TobaccoBasic tobaccoBasic){
        tobaccoService.modifyTobacco(tobaccoBasic);
        return Result.success("基本信息编辑成功");
    }

    //修改单料烟详细信息
    @PutMapping("/tobacco/detail")
    public Result modifyTobaccoDetailInfo(@RequestBody TobaccoDetail tobaccoDetail){
        tobaccoDetailMapper.updateById(tobaccoDetail);
        return Result.success("详细信息编辑成功");
    }

    //单料烟分页查询
    @GetMapping("/tobacco/page")
    public Result getTobaccoList( PageTobacco pageTobacco){
        PageResult pageResult =tobaccoService.getTobaccoList(pageTobacco);
        return Result.success(pageResult);
    }

    //根据id获单料烟基本信息
    @GetMapping("/tobacco/basic")
    public Result getTobaccoBasicInfoById(Integer id){
        TobaccoBasic tobaccoBasic = tobaccoService.getTobaccoBasicInfoById(id);
        return Result.success(tobaccoBasic);
    }

    //根据id获单料烟详细信息
    @GetMapping("/tobacco/detail")
    public Result getTobaccoDetailInfoById(Integer id){
        TobaccoDetail tobaccoDetail = tobaccoDetailMapper.selectById(id);
        log.info("tobaccoDetail = {}",tobaccoDetail);
        return Result.success(tobaccoDetail);
    }

    //根据rfid查找单料烟位置
    @PostMapping("/tobacco/show")
    public Result showTobaccoPlace(@RequestBody List<Long> rfidList){
        log.info("单料烟查询:{}",rfidList);
        executor.submit(() ->RfidUtils.search(rfidList));
        //从数据库中查找rfid的信息（用作前端位置的显示）
        if (rfidList.isEmpty()){
            return Result.success("没有数据");
        }
        List<Rfid> list = deskMapper.selectRfidInfo(rfidList);
        return Result.success(list);
    }

    //单料烟数据导出
    @GetMapping("/tobacco/download")
    public Result downLoad(DownloadParam param,HttpServletResponse response) {
        tobaccoService.downloadTobaccoData(param,response);
        return Result.success("数据导出成功");
    }

    //不足单料烟数据导出
    @GetMapping("/tobacco/short/download")
    public Result downLoadShort(HttpServletResponse response) {
        tobaccoService.downloadShort(response);
        return Result.success("数据导出成功");
    }


    //根据rfid记录不足的单料烟信息
    @PostMapping("/tobacco/short")
    public Result recordTobaccoShort(@RequestBody List<Long> rfidList){
        List<TobaccoBasic> list = tobaccoService.recordTobaccoShort(rfidList);
        return Result.success(list);
    }

    //查询所有的不足单料烟信息
    @GetMapping("/tobacco/short")
    public Result getTobaccoShort(){
        List<TobaccoBasic> list = tobaccoService.getTobaccoShort();
        return Result.success(list);
    }

    //根据rfid删除不足单料烟信息
    @DeleteMapping("/tobacco/short")
    public Result delTobaccoShort( Long rfid){
        tobaccoService.delTobaccoShort(rfid);
        return Result.success("删除成功");
    }

    //单料烟详细数据导入
    @PostMapping("/tobacco/detail/upload")
    public Result uploadTobacco(MultipartFile file) throws IOException {
        log.info("单料烟数据上传:{}",file);
        tobaccoService.uploadTobaccoDetail(file);
        return Result.success("单料烟详细信息上传成功");
    }

    //单料烟推荐
    @PostMapping("/tobacco/recommend")
    public Result recommendTobacco(@RequestBody RecommendBody recommendBody){
        log.info("单料烟推荐:{}",recommendBody);
        return Result.success("推荐成功");
    }
}
